import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vc_package/widget/extension.dart';
import 'package:vc_package/widget/image_view.dart';

import '../common/common.dart';
import '../common/enum/BaseState.dart';
import '../common/widget/bitmap_widget.dart';
import 'VCPackage.dart';
import '../utils/base_color_utils.dart';
import 'text_view.dart';

class ToolBar extends StatefulWidget {

  final NetState netState;
  final PlaceHolderType placeHolderType;
  final Widget? child;
  final String? title;
  final double? titleSize;
  final Widget? titleWidget;
  final Widget? floatingActionButton;
  final FloatingActionButtonLocation? floatingActionButtonLocation;
  final PreferredSizeWidget? appBar;
  final bool? iosBack;
  final VoidCallback? backClick;
  final Color? backgroundColor;
  final Color? appbarColor;
  final List<Widget>? actions;
  final bool? resizeToAvoidBottomPadding;
  final double? elevation;
  final bool? darkStatusText;
  final double? toolbarHeight;
  final String? toolbarSrc;
  final Color? toolbarStartBgColor;
  final Color? toolbarEndBgColor;
  final Alignment? toolbarStartBgColorAlignment;
  final Alignment? toolbarEndBgColorAlignment;
  final Widget? toolbarSubView;
  final double? marginToolbarTop;
  final bool? centerTitle;
  final WillPopCallback? onWillPop;
  final Widget? leadingIcon;
  final bool? noAppBar;
  final Widget? leading;
  final bool? noBack;
  final Key? key;
  final double? titleSpacing;
  final Widget? dragView;
  final DragStartBehavior? drawerDragStartBehavior;
  final Widget? drawer;
  final double? drawerEdgeDragWidth;
  final bool? drawerEnableOpenDragGesture;
  final Color? drawerScrimColor;
  final Color? foregroundColor;
  final Widget? endDrawer;
  final bool? endDrawerEnableOpenDragGesture;
  final DrawerCallback? onDrawerChanged;
  final DrawerCallback? onEndDrawerChanged;
  final bool? extendBodyBehindAppBar;

  ToolBar({
    required this.child,
    this.key,
    this.netState = NetState.loadingState,
    this.title = '',
    this.placeHolderType = PlaceHolderType.noPlaceHolder,
    this.titleSize,
    this.titleWidget,
    this.appBar,
    this.iosBack = false,
    this.backClick,
    this.backgroundColor,
    this.foregroundColor,
    this.appbarColor = Colors.white,
    this.actions,
    this.resizeToAvoidBottomPadding,
    this.elevation = 0.5,
    this.darkStatusText = true,
    this.toolbarHeight = -1,
    this.toolbarSrc,
    this.toolbarStartBgColor,
    this.toolbarEndBgColor,
    this.toolbarEndBgColorAlignment,
    this.toolbarStartBgColorAlignment,
    this.toolbarSubView,
    this.marginToolbarTop,
    this.floatingActionButton,
    this.floatingActionButtonLocation,
    this.centerTitle = true,
    this.onWillPop,
    this.leadingIcon,
    this.noAppBar,
    this.leading,
    this.noBack,
    this.titleSpacing,
    this.dragView,
    this.drawerDragStartBehavior,
    this.drawer,
    this.drawerEdgeDragWidth,
    this.drawerEnableOpenDragGesture,
    this.drawerScrimColor,
    this.endDrawer,
    this.endDrawerEnableOpenDragGesture,
    this.onDrawerChanged,
    this.onEndDrawerChanged,
    this.extendBodyBehindAppBar = false,
  }) : super(key: key);

  @override
  ToolBarState createState() => ToolBarState();
}

class ToolBarState extends State<ToolBar> {
  @override
  Widget build(BuildContext context) {
    PreferredSizeWidget toolbar = null == widget.appBar
        ? AppBar(
      actions: widget.actions,
      elevation: widget.elevation,
      titleSpacing:
      widget.titleSpacing ?? NavigationToolbar.kMiddleSpacing,
      surfaceTintColor: ColorUtils.white,
      leading: widget.noBack == true
          ? Container()
          : widget.leading ?? (widget.iosBack == true || null != widget.leadingIcon
          ? IconButton(
        icon: null != widget.leadingIcon
            ? widget.leadingIcon!
            : const Icon(
          Icons.arrow_back_ios,
        ),
        iconSize: 22,
        color: Color(widget.darkStatusText == true
            ? 0xff50525c
            : 0xffffffff),
        onPressed: widget.backClick ??
            (null != VCPackage.share.onTapToolbarBack
                ? () {
              VCPackage.share.onTapToolbarBack
                  ?.call(context);
            }
                : () async {
              if (Navigator.canPop(context)) {
                return Navigator.of(context).pop();
              } else {
                return await SystemNavigator.pop();
              }
            }), // null disables the button
      )
          : null),
      centerTitle: widget.centerTitle ?? true,
      backgroundColor: widget.appbarColor ?? ColorUtils.windowWhite,
      iconTheme: IconThemeData(
          color: widget.darkStatusText == true
              ? ColorUtils.black
              : ColorUtils.white),
      title: widget.titleWidget ?? Text(
        widget.title ?? '',
        style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w600,
            color: widget.darkStatusText == true
                ? ColorUtils.blackNormal
                : ColorUtils.white),
        textAlign: TextAlign.center,
      ),
    )
        : widget.appBar!;

    var body2 = null == widget.marginToolbarTop
        ? widget.child
        : Container(
      margin: EdgeInsets.only(top: widget.marginToolbarTop ?? 0),
      child: widget.child,
    );
    Widget? resultWidget;
    switch (widget.netState) {
      case NetState.error404State:
        resultWidget = const BitmapWidget(status: PageStatus.noNet,);
        break;
      case NetState.emptyDataState:
        resultWidget = const BitmapWidget(status: PageStatus.noContent,);
        break;
      case NetState.errorShowRefresh:
        resultWidget = const BitmapWidget(status: PageStatus.systemError,);
        break;
      case NetState.timeOutState:
        resultWidget = const BitmapWidget(status: PageStatus.netError,);
        break;
      case NetState.loadingState:
        if (widget.placeHolderType == PlaceHolderType.gridViewPlaceHolder) {
          resultWidget = const GridViewPlaceHolder();
        } else if (widget.placeHolderType == PlaceHolderType.listViewPlaceHolder) {
          resultWidget = const ListViewPlaceHolder();
        } else if (widget.placeHolderType == PlaceHolderType.staggeredGridPlaceHolder) {
          resultWidget = const StaggeredGridPlaceHolder();
        } else if (widget.placeHolderType == PlaceHolderType.detailPlaceHolder) {
          resultWidget = const DetailPlaceHolder();
        } else {
          resultWidget = null == widget.dragView
              ? body2
              : Stack(
            children: [body2!, widget.dragView!],
          );
        }
        break;
      case NetState.unknown:
        resultWidget = const BitmapWidget(status: PageStatus.systemError,);
        break;
      case NetState.cancelRequest:
        resultWidget = const SizedBox();
        break;
      case NetState.dataSuccessState:
        resultWidget = null == widget.dragView
            ? body2
            : Stack(
          children: [body2!, widget.dragView!],
        );
        break;
    }



    var scaffold = Scaffold(
      key: widget.key,
      // resizeToAvoidBottomPadding: widget.resizeToAvoidBottomPadding,
      resizeToAvoidBottomInset: widget.resizeToAvoidBottomPadding ?? false,
      backgroundColor: widget.backgroundColor ?? ColorUtils.windowWhite,
      drawer: widget.drawer,
      extendBodyBehindAppBar: widget.extendBodyBehindAppBar ?? false,
      drawerDragStartBehavior:
      widget.drawerDragStartBehavior ?? DragStartBehavior.start,
      drawerEdgeDragWidth: widget.drawerEdgeDragWidth,
      drawerEnableOpenDragGesture: widget.drawerEnableOpenDragGesture ?? true,
      drawerScrimColor: widget.drawerScrimColor,
      endDrawer: widget.endDrawer,
      endDrawerEnableOpenDragGesture:
      widget.endDrawerEnableOpenDragGesture ?? true,
      onDrawerChanged: widget.onDrawerChanged,
      onEndDrawerChanged: widget.onEndDrawerChanged,
      appBar: -1 == widget.toolbarHeight
          ? toolbar
          : PreferredSize(
        preferredSize: Size.fromHeight(widget.toolbarHeight ?? 0),
        child: (null == widget.toolbarSrc &&
            (null == widget.toolbarStartBgColor &&
                null == widget.toolbarEndBgColor))
            ? toolbar
            : Container(
          decoration: (null != widget.toolbarStartBgColor ||
              null != widget.toolbarEndBgColor)
              ? BoxDecoration(
            gradient: LinearGradient(
              colors: [
                widget.toolbarStartBgColor ??
                    widget.toolbarEndBgColor!,
                widget.toolbarEndBgColor ??
                    widget.toolbarStartBgColor!
              ],
              begin: widget.toolbarStartBgColorAlignment ??
                  Alignment.centerLeft,
              end: widget.toolbarEndBgColorAlignment ??
                  Alignment.centerRight,
            ),
//                              borderRadius: BorderRadius.circular(widget.radius)
          )
              : BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
                      source(widget.toolbarSrc ?? '')),
                  fit: BoxFit.fill)),
          width: double.infinity,
          height: double.infinity,
          child: null == widget.toolbarSubView
              ? toolbar
              : Column(
            children: widget.noAppBar == true
                ? <Widget>[
              const PreferredSize(
                preferredSize: Size.fromHeight(0),
                child: SizedBox(
                  height: 25,
                ),
              ),
              Expanded(
                child: widget.toolbarSubView!,
              )
            ]
                : <Widget>[
              toolbar,
              Expanded(
                child: widget.toolbarSubView!,
              )
            ],
          ),
        ),
      ),
      body: body2,
      floatingActionButton: widget.floatingActionButton,
      floatingActionButtonLocation: widget.floatingActionButtonLocation,
    );
    return widget.noBack != true && null == widget.onWillPop
        ? scaffold
        : WillPopScope(
        onWillPop: widget.noBack == true && widget.onWillPop == null
            ? () async {
          return false;
        }
            : widget.onWillPop,
        child: scaffold);
  }
}